package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.app.utils.PlaySoundXfUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.lepu.app.fun.index.bean.BeanIndexBookItem;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class IndexBookContentActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {

	private CustomTopBarNew topbar;
	private WebView webview;
	private ImageView listImage;
	private ImageView upImage;
	private ImageView downImage;
	private ImageView readImage;
	private PlaySoundXfUtil playSound;
	private String readContent = "";
	
	private int book;  //第几本书
	private int index;  //下标
	
    private ArrayList<BeanIndexBookItem> bookList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_book_content_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		playSound.stopSound();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void init() {
		// topbar
		topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("");
		topbar.setonTopbarNewLeftLayoutListener(this);

		//String path = getIntent().getStringExtra("path");
		
		webview = (WebView) this.findViewById(R.id.webview);
		webview.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 禁止长按复制文本
				android.util.Log.d("DDD", "DDD");
				return true;
			}
		});

		WebSettings settings = webview.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setLoadWithOverviewMode(true);
		webview.setWebChromeClient(new WebChromeClient());
		
		listImage = (ImageView) findViewById(R.id.book_list_image);
		upImage = (ImageView) findViewById(R.id.book_up_image);
		downImage = (ImageView) findViewById(R.id.book_down_image);
		readImage = (ImageView) findViewById(R.id.book_read_image);
		
		listImage.setOnClickListener(viewClick);
		upImage.setOnClickListener(viewClick);
		downImage.setOnClickListener(viewClick);
		readImage.setOnClickListener(viewClick);
		
		book = getIntent().getIntExtra("book", 1);
		index = getIntent().getIntExtra("index", 0);
		
		if(book == 1){
			bookList = BeanIndexBookItem.getBook1(this);
		}else if(book == 2){
			bookList = BeanIndexBookItem.getBook2(this);
		}
		
		setText(index);

	}
	
	View.OnClickListener viewClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.book_list_image:
				finish(true);
				break;
			case R.id.book_up_image:
				if(index > 0){
					index = index -1;
					setText(index);
				}
				break;
			case R.id.book_down_image:
				if(index < bookList.size()-1){
					index = index + 1;
					setText(index);
				}
				break;
			case R.id.book_read_image:
				playSound.playSound(readContent, readImage);
				break;

			default:
				break;
			}
		}
	};
	
	   private void setText(int index) {
		   BeanIndexBookItem item = bookList.get(index);
		   webview.loadUrl(item.loadPath);
		   topbar.setTopbarTitle(item.chapter);
		   readContent = item.readContent;
		   
		   //初始化语音
		   if(playSound != null){
			   //释放上次语音
			   playSound.stopSound();
		   }
		   playSound = new PlaySoundXfUtil(IndexBookContentActivity.this);
		   readImage.setSelected(false);
		   
	    }

}
